<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/29
 * Time: 下午8:29
 */

class GoodsService extends Service {

    public static function indexHeader(){
        $getHeader = array(
            'RecDate'   => array('text' => '日期'),
            'ItemId'    => array('text' => '道具ID'),
            'ItemName'  => array('text' => '道具名称'),
            'GetNumber'   => array('text' => '产出数量','rule'=>'numeric'),
            'GetUser'   => array('text' => '人数','rule'=>'numeric'),
            'GetCnt'   => array('text' => '人次','rule'=>'numeric'),
            'GetRate'   => array('text' => '比例','rule'=>'numeric'),
        );
        $useHeader = array(
            'RecDate'   => array('text' => '日期'),
            'ItemId'    => array('text' => '道具ID'),
            'ItemName'  => array('text' => '道具名称'),
            'UsedNumber'   => array('text' => '消耗数量','rule'=>'numeric'),
            'UsedUser'   => array('text' => '人数','rule'=>'numeric'),
            'UsedCnt'   => array('text' => '人次','rule'=>'numeric'),
            'UsedRate'   => array('text' => '比例','rule'=>'numeric'),
        );

        return array(
            'get'   =>  $getHeader,
            'used'  =>  $useHeader
        );
    }

    public static function exportHeader(){
        $header = array(
            'RecDate'   => array('text' => '日期'),
            'Type'      => array('text' => '产出/消耗'),
            'ItemId'    => array('text' => '道具ID'),
            'ItemName'  => array('text' => '道具名称'),
            'Number'    => array('text' => '相关货币','rule'=>'numeric'),
            'User'      => array('text' => '相关人数','rule'=>'numeric'),
            'Cnt'       => array('text' => '相关人次','rule'=>'numeric'),
            'Rate'      => array('text' => '相关比例','rule'=>'numeric'),
        );

        return $header;
    }

    public static function indexData($params){
        $data = ModGoods::indexData($params);
        
        $new_data = array();
        foreach ($data as $key=>$val){
        	$new_data[$val['ItemId']]['RecDate'] = $val['RecDate'];
        	$new_data[$val['ItemId']]['ItemId'] = $val['ItemId'];
        	$new_data[$val['ItemId']]['ItemName'] = $val['ItemName'];
        	
        	$new_data[$val['ItemId']]['GetNumber'] += $val['GetNumber'];
        	$new_data[$val['ItemId']]['UsedNumber'] += $val['UsedNumber'];
        	$new_data[$val['ItemId']]['GetUser'] += $val['GetUser'];
        	$new_data[$val['ItemId']]['UsedUser'] += $val['UsedUser'];
        	$new_data[$val['ItemId']]['GetCnt'] += $val['GetCnt'];
        	$new_data[$val['ItemId']]['UsedCnt'] += $val['UsedCnt']; 	
        }
        $data = $new_data;

//        $goodsDict = PubDict::dict($params['g'],"itemaction");
//        PubFunc::parseDict($data,array("ItemId"=>$goodsDict));
        $data = self::splitData($data);

        $allParamsGet = array(
            "total" => array("RecDate"=>"Total"),
            "sumKey"=>array("GetNumber")
        );
        $getRateConfig = array(
            "GetNumber"   =>  array("to" =>  "GetRate",$allParamsGet),
        );

        $allParamsUsed = array(
            "total" => array("RecDate"=>"Total"),
            "sumKey"=>array("UsedNumber")
        );
        $usedRateConfig = array(
            "UsedNumber"   =>  array("to" =>  "UsedRate",$allParamsUsed),
        );

        PubStat::setRate($data['get'],$getRateConfig,$allParamsGet,$getTotal);
        PubStat::setRate($data['used'],$usedRateConfig,$allParamsUsed,$usedTotal);
        $data['getTotal'] = $getTotal;
        $data['usedTotal'] = $usedTotal;
        return $data;
    }

    public static function indexChart($data, $params){
        $chartDataGet = $chartDataUsed = array();

        $getLabel = PubFunc::t('物品产出分布');
        $usedLabel = PubFunc::t('物品消耗分布');

        $pubSort = new PubMultiSort();
        $pubSort->setSort('GetNumber','desc');
        $data['get'] = PubStat::getTop($pubSort->getResult($data['get']),20);

        $pubSort = new PubMultiSort();
        $pubSort->setSort('UsedNumber','desc');
        $data['used'] = PubStat::getTop($pubSort->getResult($data['used']),20);

        foreach($data['get'] as $v){
            $key = !empty($v['ItemName']) ? $v['ItemName'] : $v['ItemId'];
            $chartDataGet[$getLabel][$key] = intval($v['GetNumber']);
        }

        foreach($data['used'] as $v){
            $key = !empty($v['ItemName']) ? $v['ItemName'] : $v['ItemId'];
            $chartDataUsed[$usedLabel][$key] = intval($v['UsedNumber']);
        }

        return array(
            'get'=> array(
                'data'   => $chartDataGet
            ),
            'used'=> array(
                'data'   => $chartDataUsed
            ),
        );
    }

    public static function splitData($data){
        $get = array();
        $used = array();
        foreach($data as $v){
            if($v['UsedCnt']){
                $used[] = $v;
            }
            if($v['GetCnt']){
                $get[] = $v;
            }
        }

        return array(
            "get"   =>  $get,
            "used"  =>  $used
        );
    }

} 